package com.ls.service.impl;

import com.ls.entity.Message;
import com.ls.mapper.MessageMapper;
import com.ls.service.MessageService;
import com.ls.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author ls
 * @date 2020/12/30
 **/
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Override
    public List<Message> queryConversations(int userId, int startIndex, int pageSize) {
        return messageMapper.queryConversations(userId, startIndex, pageSize);
    }

    @Override
    public int queryConversationCount(int userId) {
        return messageMapper.queryConversationCount(userId);
    }

    @Override
    public List<Message> queryLetters(String conversationId, int startIndex, int pageSize) {
        return messageMapper.queryLetters(conversationId, startIndex, pageSize);
    }

    @Override
    public int queryLetterCount(String conversationId) {
        return messageMapper.queryLetterCount(conversationId);
    }

    @Override
    public int queryLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.queryLetterUnreadCount(userId, conversationId);
    }

    @Override
    public void addMessage(Message message) {
        // 过滤敏感词,转义html标签
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        messageMapper.addMessage(message);
    }

    @Override
    public void readMessage(List<Integer> ids) {
        messageMapper.updateMessage(ids, 1);
    }

    @Override
    public Message queryLatestNotice(int userId, String topic) {
        return messageMapper.queryLatestNotice(userId, topic);
    }

    @Override
    public int queryNoticeCount(int userId, String topic) {
        return messageMapper.queryNoticeCount(userId, topic);
    }

    @Override
    public int queryNoticeUnreadCount(int userId, String topic) {
        return messageMapper.queryNoticeUnreadCount(userId, topic);
    }

    @Override
    public List<Message> queryNotices(int userId, String topic, int startIndex, int pageSize) {
        return messageMapper.queryNotices(userId, topic, startIndex, pageSize);
    }
}
